package com.punch.punchtable.controller;


import com.punch.punchtable.entity.Sumup;
import com.punch.punchtable.service.SumupService;
import com.punch.punchtable.utils.ExcelUtil;
import com.punch.punchtable.vo.PageResponseVO;
import com.punch.punchtable.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/sumup")
public class SumupController {


    @Autowired
    private SumupService service;


    @GetMapping("/page")
    @ResponseBody
    public PageResponseVO page(Sumup sumup, Integer page, Integer limit,
                               @RequestParam(name = "date",defaultValue = "",required = false) String date){

        if (!StringUtils.isEmpty(date)){
            String[] split = date.split("-");
            sumup.setYear(Long.parseLong(split[0]));
            sumup.setMonth(Long.parseLong(split[1]));
        }

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("username",ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("usernum",ExampleMatcher.GenericPropertyMatchers.contains());

        if (StringUtils.isEmpty(sumup.getDept())){
            sumup.setDept(null);
        }

        if (StringUtils.isEmpty(sumup.getUsername())){
            sumup.setUsername(null);
        }

        if (StringUtils.isEmpty(sumup.getUsernum())){
            sumup.setUsernum(null);
        }
        Example<Sumup> of = Example.of(sumup,matcher);
        PageRequest pageRequest = new PageRequest(page - 1, limit, new Sort(Sort.Direction.ASC, "sid"));
        Page<Sumup> byPage = service.findByPage(of, pageRequest);
        return PageResponseVO.response(byPage);
    }

    @GetMapping("/dept")
    @ResponseBody
    public ResponseVO dept(){
        List<String> dept = service.findDept();
        return ResponseVO.ok(dept);
    }



    @GetMapping("/export")
    public void export(Sumup sumup,
                       @RequestParam(name = "date",defaultValue = "",required = false) String date,
                       HttpServletResponse response){

        if (!StringUtils.isEmpty(date)){
            String[] split = date.split("-");
            sumup.setYear(Long.parseLong(split[0]));
            sumup.setMonth(Long.parseLong(split[1]));
        }

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("username",ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("usernum",ExampleMatcher.GenericPropertyMatchers.contains());

        if (StringUtils.isEmpty(sumup.getDept())){
            sumup.setDept(null);
        }


        if (StringUtils.isEmpty(sumup.getUsername())){
            sumup.setUsername(null);
        }

        if (StringUtils.isEmpty(sumup.getUsernum())){
            sumup.setUsernum(null);
        }


        Example<Sumup> of = Example.of(sumup,matcher);
        List<Sumup> all = service.findAll(of);

        List<String> titleList = Arrays.asList("编号","年", "月", "用户名", "工号", "部门", "出勤次数", "迟到次数", "早退次数","是否领导");

        List<List<String>> dataList = new ArrayList<>();
        for (Sumup a : all) {
            List<String> list = new ArrayList<>();
            list.add(String.valueOf(a.getSid()));
            list.add(String.valueOf(a.getYear()));
            list.add(String.valueOf(a.getMonth()));
            list.add(String.valueOf(a.getUsername()));
            list.add(String.valueOf(a.getUsernum()));
            list.add(String.valueOf(a.getDept()));
            list.add(String.valueOf(a.getUserattendance()));
            list.add(String.valueOf(a.getUserlate()));
            list.add(String.valueOf(a.getUserearly()));
//            list.add(String.valueOf(a.getRole()));
            dataList.add(list);

        }

        ExcelUtil.uploadExcelAboutUser(response,
                String.format("统计_%d.xlsx",System.currentTimeMillis()),
                titleList,
                dataList);
    }





}
